﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace kis.api.orm
{
    /// <summary>
    /// 
    /// </summary>
    public class dbAPI
    {
        /// <summary>
        /// 
        /// </summary>
        public string connectString { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public SqlSugarClient db { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public dbAPI()
        {
            connectString = "DataSource=" + AppDomain.CurrentDomain.BaseDirectory + @"db\data.db;Pooling=true;FailIfMissing=false";

            var config = new ConnectionConfig()
            {
                DbType = DbType.Sqlite,
                ConnectionString = this.connectString

            };

            this.db = new SqlSugarClient(config);

            this.db.Aop.OnLogExecuting = OnLogExecuting;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pars"></param>
        public virtual void OnLogExecuting(string sql, SugarParameter[] pars)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine(sql);
            sb.AppendLine(this.db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
        }
    }
}
